import { takeLatest, all } from 'redux-saga/effects'
import API from '../Services/Api'
import FixtureAPI from '../Services/FixtureApi'
import DebugConfig from '../Config/DebugConfig'

/* ------------- Types ------------- */

import { CommentTypes } from '../Redux/CommentRedux'
import { CouponTypes } from '../Redux/CouponRedux'
import { CityTypes } from '../Redux/CityRedux'
import { StartupTypes } from '../Redux/StartupRedux'
import { LoginTypes } from '../Redux/LoginRedux'
import { RegisterTypes } from '../Redux/RegisterRedux'
import { CheckMobileTypes } from '../Redux/CheckMobileRedux'
import { ValidcodeTypes } from '../Redux/ValidcodeRedux'
import { UserTypes } from '../Redux/UserRedux'
import { AddressTypes } from '../Redux/AddressRedux'
import { CommodityTypes } from '../Redux/CommodityRedux'
import { CommodityDetailTypes } from '../Redux/CommodityDetailRedux'
import { CommodityCategoryTypes } from '../Redux/CommodityCategoryRedux'
import { ServerTypes } from '../Redux/ServerRedux'
import { ServerDetailTypes } from '../Redux/ServerDetailRedux'
import { ServerCommodityTypes } from '../Redux/ServerCommodityRedux'
import { AdTypes } from '../Redux/AdRedux'
import { CallTypes } from '../Redux/CallRedux'
import { OrderTypes } from '../Redux/OrderRedux'
import { OrderDetailTypes } from '../Redux/OrderDetailRedux'
import { PromotionTypes } from '../Redux/PromotionRedux'
import { PageTypes } from '../Redux/PageRedux'
import { TodayRecommendTypes } from '../Redux/TodayRecommendRedux'
import { PayTypes } from '../Redux/PayRedux'
import { AlipayTypes } from '../Redux/AlipayRedux'
import { HomeSmallBannerTypes } from '../Redux/HomeSmallBannerRedux'
import { VipTypes } from '../Redux/VipRedux'
import { WalletTypes } from '../Redux/WalletRedux'

/* ------------- Sagas ------------- */

import { getComment } from './CommentSagas'
import { getCoupon } from './CouponSagas'
import { getCity } from './CitySagas'

import { startup } from './StartupSagas'
import { getLogin } from './LoginSagas'
import { getRegister } from './RegisterSagas'
import { getCheckMobile } from './CheckMobileSagas'
import { getValidcode } from './ValidcodeSagas'
import { getUser, updateUser } from './UserSagas'
import { getCommodity } from './CommoditySagas'
import { getCommodityDetail } from './CommodityDetailSagas'
import { getCommodityCategory } from './CommodityCategorySagas'
import { getServer } from './ServerSagas'
import { getServerDetail } from './ServerDetailSagas'
import { getServerCommodity } from './ServerCommoditySagas'
import { getAd } from './AdSagas'
import { getAllAddress, createAddress, updateAddress, deleteAddress } from './AddressSagas'
import { getCall } from './CallSagas'
import { getOrder } from './OrderSagas'
import { getOrderDetail, cancelOrder, deleteOrder } from './OrderDetailSagas'
import { getPromotion } from './PromotionSagas'
import { getPage } from './PageSagas'
import { getTodayRecommend } from './TodayRecommendSagas'
import { getPay } from './PaySagas'
import { getAlipay } from './AlipaySagas'
import { getHomeSmallBanner } from './HomeSmallBannerSagas'
import { getVip } from './VipSagas'
import { getWallet } from './WalletSagas'

/* ------------- API ------------- */

// The API we use is only used from Sagas, so we create it here and pass along
// to the sagas which need it.
const api = DebugConfig.useFixtures ? FixtureAPI : API.create()

/* ------------- Connect Types To Sagas ------------- */

export default function * root () {
  yield all([
    takeLatest(CommentTypes.COMMENT_REQUEST, getComment, api),
    takeLatest(CouponTypes.COUPON_REQUEST, getCoupon, api),
    takeLatest(CityTypes.CITY_REQUEST, getCity, api),

    takeLatest(StartupTypes.STARTUP, startup),
    takeLatest(LoginTypes.LOGIN_REQUEST, getLogin, api),
    takeLatest(RegisterTypes.REGISTER_REQUEST, getRegister, api),
    takeLatest(CheckMobileTypes.CHECK_MOBILE_REQUEST, getCheckMobile, api),
    takeLatest(ValidcodeTypes.VALIDCODE_REQUEST, getValidcode, api),
    takeLatest(UserTypes.USER_REQUEST, getUser, api),
    takeLatest(UserTypes.UPDATE_USER_REQUEST, updateUser, api),
    takeLatest(CommodityTypes.COMMODITY_REQUEST,getCommodity,api),
    takeLatest(CommodityDetailTypes.COMMODITY_DETAIL_REQUEST,
      getCommodityDetail, api),
    takeLatest(CommodityCategoryTypes.COMMODITY_CATEGORY_REQUEST,
      getCommodityCategory, api),
    takeLatest(ServerTypes.SERVER_REQUEST, getServer, api),
    takeLatest(ServerDetailTypes.SERVER_DETAIL_REQUEST, getServerDetail, api),
    takeLatest(ServerCommodityTypes.SERVER_COMMODITY_REQUEST, getServerCommodity, api),
    takeLatest(AdTypes.AD_REQUEST, getAd, api),

    // address
    takeLatest(AddressTypes.GET_ALL_REQUEST, getAllAddress, api),
    takeLatest(AddressTypes.CREATE_REQUEST, createAddress, api),
    takeLatest(AddressTypes.UPDATE_REQUEST, updateAddress, api),
    takeLatest(AddressTypes.DELETE_REQUEST, deleteAddress, api),


    takeLatest(CallTypes.CALL_REQUEST, getCall, api),
    takeLatest(OrderTypes.ORDER_REQUEST, getOrder, api),
    takeLatest(OrderDetailTypes.ORDER_DETAIL_REQUEST, getOrderDetail, api),
    takeLatest(OrderDetailTypes.CANCEL_REQUEST, cancelOrder, api),
    takeLatest(OrderDetailTypes.ORDER_DELETE_REQUEST, deleteOrder, api),
    takeLatest(PromotionTypes.PROMOTION_REQUEST, getPromotion, api),
    takeLatest(PageTypes.PAGE_REQUEST, getPage, api),
    takeLatest(TodayRecommendTypes.TODAY_RECOMMEND_REQUEST, getTodayRecommend, api),
    takeLatest(PayTypes.PAY_REQUEST, getPay, api),
    takeLatest(AlipayTypes.ALIPAY_REQUEST, getAlipay, api),
    takeLatest(HomeSmallBannerTypes.HOME_SMALL_BANNER_REQUEST, getHomeSmallBanner, api),
    takeLatest(VipTypes.VIP_REQUEST, getVip, api),
    takeLatest(WalletTypes.WALLET_REQUEST, getWallet, api),
  ])
}
